package dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.*;
import pojo.*;
import util.EntityManagerUtil;

public class RouteDao {
	public static List<Route> getRoutes() {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Query query = em.createQuery("SELECT c FROM Route c ");
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		List<Route> rnames = query.getResultList();
		em.close();
		return rnames;
	}

	public static boolean addRoute(Route route) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			if (!getRouteNoCacheForBool(route.getRname())) {
				em.getTransaction().begin();
				em.persist(route);
				em.getTransaction().commit();
				em.close();
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public static Route getRoute(Route route) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Route routeFind = new Route();
		routeFind = em.find(Route.class, route.getRname());
		if (routeFind != null) {
			em.close();
			return routeFind;
		} else
			return null;
	}

	// ��ѯ·����Ϣ�޻��� success
	public static Route getRouteNoCache(Route route) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Route c where c.rname=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Route routeFind;
		try {
			routeFind = (Route) query.setParameter("id", route.getRname()).getSingleResult();
			em.close();
			return routeFind;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			em.close();
			return null;
		}

	}

	public static Route getRouteNoCache(String rname) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Route c where c.rname=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Route routeFind;
		try {
			routeFind = (Route) query.setParameter("id", rname).getSingleResult();
			em.close();
			return routeFind;
		} catch (Exception e) {
			em.close();
			System.out.println("查找route失败");
			return null;
			// TODO Auto-generated catch block

		}
	}

	public static boolean getRouteNoCacheForBool(String rname) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Route c where c.rname=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Route routeFind;
		try {
			routeFind = (Route) query.setParameter("id", rname).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			em.close();
			System.out.println("查找route失败");
			return false;
			// TODO Auto-generated catch block

		}
	}

	public static boolean delRoute(Route route) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Route routeFind = getRouteNoCache(route);
			if (routeFind != null) {
				Route delRoute = em.find(Route.class, route.getRname());
				em.getTransaction().begin();
				List<Vroute> vroutes = delRoute.getVroutes();
				for (Vroute v : vroutes) {
					VrouteDao.delVroute(v);
				}
				List<Rorder> rorders = delRoute.getRorders();
				for (Rorder r : rorders) {
					RorderDao.delRorder(r);
				}
				em.remove(delRoute);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public static void refreshMoney(Route route) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Route routeFind = getRouteNoCache(route);
			if (routeFind != null) {
				Route upRoute = em.find(Route.class, route.getRname());
				em.getTransaction().begin();
				List<Vroute> vroutes = route.getVroutes();
				int money = 0;
				for(Vroute vroute : vroutes) {
					View view = ViewDao.getView(vroute.getView().getName());
					money = money+view.getPrice();
				}
				System.out.println("money = "+money);
				upRoute.setMoney(money);
				em.merge(upRoute);
				em.getTransaction().commit();
				em.close();
			} else {
			}
		} catch (Exception e) {
		}

	}

	public static boolean updateRoute(Route route) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Route routeFind = getRouteNoCache(route);
			if (routeFind != null) {
				Route upRoute = em.find(Route.class, route.getRname());
				em.getTransaction().begin();
				upRoute.setInfo(route.getInfo());
				upRoute.setDays(route.getDays());
				upRoute.setMoney(route.getMoney());
				em.persist(upRoute);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

}
